package controller;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Vector;

import javax.swing.table.DefaultTableModel;

import view.ClientWindow;

public class ClientWindowController {

	public void findClient() {

		String family = ClientWindow.getTextField();
		String name = ClientWindow.getTextField_1();
		String surname = ClientWindow.getTextField_2();
		String phoneNumber = ClientWindow.getTextField_3();

		String fio = family.concat(" ").concat(name).concat(" ")
				.concat(surname);

		try {
			Connection conn = DatabaseConnection.getInstance().getConnection();

			Statement st = conn.createStatement();
			ResultSet rs = st.executeQuery(String.format(
					"SELECT * FROM clients WHERE fio=\"%s\" OR tel=%s", fio,
					phoneNumber));
			int numColumns = rs.getMetaData().getColumnCount();
			Vector column = new Vector<>();
			Vector data_DB = new Vector<>();

			for (int i = 1; i <= numColumns; i++) {
				column.add(rs.getMetaData().getColumnName(i));
				while (rs.next()) {
					Vector row = new Vector<>();
					for (int i2 = 1; i2 <= numColumns; i2++) {
						row.add(rs.getString(i2));
					}
					data_DB.add(row);
				}
			}
			rs.close();
			st.close();
			ClientWindow.table.setModel(new DefaultTableModel(data_DB, column));
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

	}

}
